package pro.softzhang.algo.lc200;

import pro.softzhang.algo.flag.difficulty.DRed;
import pro.softzhang.algo.flag.knowledge.KDynamicPlanning;
import pro.softzhang.algo.flag.theme.TRobber;
import pro.softzhang.algo.flag.x.XOptimal;

@KDynamicPlanning
@DRed
@TRobber
public class LC213_HouseRobberII {
    public static void main(String[] args) {
        int[] nums = new int[]{0,0};
        int max = new Solution().rob(nums);
        System.out.println(max);
    }

    @XOptimal
    static class Solution {

        public int rob(int[] nums) {
            if(nums.length == 1) {
                return nums[0];
            }
            return Math.max(rob0(nums, 0, nums.length - 2), rob0(nums, 1, nums.length - 1));
        }

        private int rob0(int[] nums, int startIdx, int endIdx) {
            int[] arr = new int[2];
            arr[0] = nums[startIdx];
            if(startIdx < endIdx) {
                arr[1] = Math.max(nums[startIdx], nums[startIdx + 1]);
            }

            for (int i = startIdx + 2; i <= endIdx; i++) {
                int j = i - startIdx;
                arr[j % 2] = Math.max(arr[(j - 1) % 2], arr[(j - 2) % 2] + nums[i]);
            }

            return arr[(endIdx - startIdx) % 2];
        }
    }
}